package cn.tansanqinger.controller.topic;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.tansanqinger.service.topic.ITopicService;
import cn.tansanqinger.util.Tools;

@Controller
@RequestMapping("/admin/topic")
public class TopicWarController {
	
	@Autowired
	private ITopicService service;
	
	@RequestMapping("/list2")
	public ModelAndView list(HttpServletRequest request, ModelAndView mv) {
		mv.setViewName("topic/list2");
		return mv;
	}
	
	@RequestMapping("/findListData2")
	@ResponseBody
	public String findListData(HttpServletRequest request) {
		try {
			return new ObjectMapper().writeValueAsString(service.selectMessage(request));
		} catch (Exception e) {}
		return null;
	}
	
	/**
	 * 添加信息
	 * @param request
	 * @param mv
	 * @return
	 */
	@RequestMapping("/goAdd2")
	public ModelAndView goAdd(HttpServletRequest request, ModelAndView mv) {
		mv.addObject("serialNumber", service.serialNumber());
		mv.setViewName("topic/goAdd2");
		return mv;
	}
	
	/**
	 * 编辑信息
	 * @param request
	 * @param mv
	 * @return
	 */
	@RequestMapping("/edit2")
	public ModelAndView edit2(HttpServletRequest request, ModelAndView mv) {
		mv.addObject("topic", service.selectIdOne(Tools.isEmpty(request.getParameter("id"))));
		mv.setViewName("topic/edit2");
		return mv;
	}
	
	/**
	 * 修改信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/modification2")
	@ResponseBody
	public Map<String, Object> modification(HttpServletRequest request){
		return service.modification2(request);
	}
	
	/**
	 * 修改状态
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateWeekend2")
	@ResponseBody
	public Map<String, Object> updateWeekend2(HttpServletRequest request){
		return service.updateStatus(Tools.isEmpty(request.getParameter("id")));
	}
}
